package com.lsf.dunlian.activity;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.google.gson.Gson;
import com.lsf.dunlian.R;
import com.lsf.dunlian.adapter.MemberListRecyclerViewAdapter;
import com.lsf.dunlian.adapter.MemberListTitleRecyclerViewAdapter;
import com.lsf.dunlian.adapter.NoticeRecyclerViewAdapter;
import com.lsf.dunlian.callback.ListClickCallback;
import com.lsf.dunlian.config.PreferenceUtil;
import com.lsf.dunlian.config.Urls;
import com.lsf.dunlian.constant.Constant;
import com.lsf.dunlian.databinding.ActivityMemberlistBinding;
import com.lsf.dunlian.databinding.ActivityNoticeBinding;
import com.lsf.dunlian.entity.AllNoticeRecordData;
import com.lsf.dunlian.entity.AllNoticeRecordEntity;
import com.lsf.dunlian.entity.BasicMember;
import com.lsf.dunlian.entity.MemberListTitleEntity;
import com.lsf.dunlian.entity.MemberListVo;

import java.io.IOException;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

public class MemberListActivity extends Activity implements ListClickCallback {
    private LayoutInflater inflater;
    private ActivityMemberlistBinding binding;
    private MemberListRecyclerViewAdapter adapter;
    private MemberListTitleRecyclerViewAdapter titleRecyclerViewAdapter;
    private List<BasicMember> list = new ArrayList<>();
    private List<MemberListTitleEntity> arrayList = new ArrayList<>();

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        BaseUtils.setStatusBar(this);
        inflater = LayoutInflater.from(this);
        binding = DataBindingUtil.inflate(inflater, R.layout.activity_memberlist, null, false);
        setContentView(binding.getRoot());
        adapter = new MemberListRecyclerViewAdapter(list, this);
        binding.recyclerView.setLayoutManager(new LinearLayoutManager(this));
        binding.recyclerView.setAdapter(adapter);
        binding.back.setOnClickListener(v -> finish());
        arrayList.add(new MemberListTitleEntity("全部", true));
        arrayList.add(new MemberListTitleEntity("潜在客户", false));
        arrayList.add(new MemberListTitleEntity("初步接触", false));
        arrayList.add(new MemberListTitleEntity("持续跟进", false));
        arrayList.add(new MemberListTitleEntity("成交客户", false));
        arrayList.add(new MemberListTitleEntity("忠诚客户", false));

        binding.fabu.setOnClickListener(v -> startActivity(new Intent(MemberListActivity.this,MemberActivity.class)));

        titleRecyclerViewAdapter = new MemberListTitleRecyclerViewAdapter(arrayList, this);
        titleRecyclerViewAdapter.setClickCallback(this);
        binding.titleRecyclerView.setLayoutManager(new LinearLayoutManager(this, RecyclerView.HORIZONTAL, false));
        binding.titleRecyclerView.setAdapter(titleRecyclerViewAdapter);
    }

    @SuppressLint("NewApi")
    private void get() {
        List<MemberListTitleEntity> collect = arrayList.stream().filter(s -> s.select).collect(Collectors.toList());
        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder().get().url(Urls.MEMBER_LIST +
                "?userId=" + PreferenceUtil.getSharedPreference(Constant.USERID)+"&searchVal="+collect.get(0).title).build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {
                String a="";
            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {
                if (response.isSuccessful()) {
                    String result = response.body().string();
                    MemberListVo vo = new Gson().fromJson(result, MemberListVo.class);
                    if (vo.code == 200 && vo.data != null) {
                        list.clear();
                        list.addAll(vo.data);
                        runOnUiThread(() -> adapter.notifyDataSetChanged());
                    }
                }
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        get();
    }

    @Override
    public void click(String val) {
        get();
    }
}
